let  middleware = []

middleware.push(function(ctx,next){
    console.log(1);
    next()
    console.log('end 1');
    
})
middleware.push(function(ctx,next){
    console.log(2);
    next()
    console.log('end 2');
});

middleware.push(function(ctx,next){
    console.log(3);
    next()
    console.log('end 3');
});

function compose(middleware){
   
    function dispatch(i){
        if(i === middleware.length) return
        let fn = middleware[i]
        const next = ()=>{
            dispatch(i+1)
        }
            fn(this, next)
    }
    dispatch(0)
}

const fn = compose(middleware)

fn()